package io.babystar.modules.business.controller;

import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.CkInEntity;
import io.babystar.modules.business.entity.CkProductEntity;
import io.babystar.modules.business.entity.TeacherEntity;
import io.babystar.modules.business.service.CkInService;
import io.babystar.modules.business.service.CkProductService;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.modules.sys.controller.AbstractController;
import io.babystar.modules.sys.entity.SysUserEntity;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 入库单
 *
 * @author rsq
 * @email rsq15234313042@163.com
 * @date 2021-03-03 10:42:29
 */
@RestController
@RequestMapping("business/ckin")
public class CkInController extends AbstractController {

    @Autowired
    private CkInService ckInService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CkProductService ckProductService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:ckin:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity sysUser = ShiroUtils.getUserEntity();
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        params.put("businessId",userEntity.getBusinessId());
        int type = sysUser.getType();
        //店长、教练只查询当前店铺的
        if(type==2||type==3){
            Long teacherId = sysUser.getParentTeacherId();
            TeacherEntity teacher = teacherService.getById(teacherId);
            params.put("shopId",teacher.getShopId()+"");
        }
        PageUtils page = ckInService.getByproductName(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:ckin:info")
    public R info(@PathVariable("id") Integer id){
		CkInEntity ckIn = ckInService.getById(id);

        return R.ok().put("ckIn", ckIn);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:ckin:save")
    public R save(@RequestBody CkInEntity ckIn){
        SysUserEntity sysUserEntity = ShiroUtils.getUserEntity();
        CkProductEntity ckProductEntity = ckProductService.getById(ckIn.getProductCode());
        //判断是否存在该商品
        if(ckProductEntity.getNum() != null && !"".equals(ckProductEntity.getNum())){
            //增加商品数量
            Integer productNum = ckProductEntity.getNum() + ckIn.getInNum();
            ckProductEntity.setNum(productNum);
            ckProductEntity.setUpdateTime(new Date());
            //若更新成功再存入库单
            if(ckProductService.updateById(ckProductEntity)){
                int type = sysUserEntity.getType();
                //机构账号
                if(type==1){
                    ckIn.setShopId(-1);
                }
                ckIn.setCreateTime(new Date());
                ckIn.setCreator(sysUserEntity.getUsername());
                ckIn.setBusinessId(sysUserEntity.getBusinessId().intValue());
                ckInService.save(ckIn);
                return R.ok();
            }else{
                return R.error(1,"库存更新失败");
            }
        }else{
            return R.error(1,"该商品不存在");
        }
    }

    /**
     * 申请-apply
     */
    @RequestMapping("/apply")
    @RequiresPermissions("business:ckin:apply")
    public R apply(@RequestBody CkInEntity ckIn){
        return ckInService.apply(ckIn);
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:ckin:update")
    public R update(@RequestBody CkInEntity ckIn){
        ckIn.setUpdateTime(new Date());
		ckInService.updateById(ckIn);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:ckin:delete")
    public R delete(@RequestBody Integer[] ids){
        List<Integer> list = Arrays.asList(ids);
        if(list!=null && list.size()==1){
            return ckInService.delete(list.get(0));
        }
        return R.ok();
    }


    /**
     * 统计入库单总数量
     */
    @RequestMapping("/queryCkInCountList")
    public R queryCkInCountList(@RequestParam Map<String, Object> params) {
        SysUserEntity userEntity = this.getUser();
        params.put("businessId", userEntity.getBusinessId());
        int type = userEntity.getType();
        if( type== 2 || type ==3){
            params.put("shopId", this.getTeacherShopId());
        }
        PageUtils page = ckInService.queryCkInCountList(params);
        return R.ok().put("page", page);
    }


}
